package com.haiermzj.server.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.haiermzj.server.Contance;
import com.haiermzj.server.MainActivity;
import com.rock.qrcodelibrary.CaptureActivity;
import com.rock.qrcodelibrary.eventbus.MessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by wanghaixin on 17/9/12.
 */
public class MyServerSocketService extends Service {
    private ServerSocket serverSocket;
    private int port = 8888;
    private String x;
    private MainActivity activity;
    Socket socket = null;

    public MyServerSocketService() {
    }

    public MyServerSocketService(Context context) throws IOException {

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

//        this.activity = (MainActivity) context;
//        try {
//            serverSocket = new ServerSocket(port, 3);
//            System.out.println("服务器启动!" + InetAddress.getLocalHost());
//
//        } catch (IOException e) {
//            e.printStackTrace();
//            Log.e("======", "onStart: IOException:" + e.getMessage());
//        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    serverSocket = new ServerSocket(port, 3);
            System.out.println("服务器启动!" + InetAddress.getLocalHost());
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while (true) {
                    try {
                        socket = serverSocket.accept();
                        Log.e("======", "服务器：New connection accepted " +
                                socket.getInetAddress() + ":" + socket.getPort());
//                    char read = (char) System.in.read();
                        InputStream inputStream = socket.getInputStream();
                        DataInputStream input = new DataInputStream(inputStream);
                        byte buffer[] = new byte[1024 * 4];
                        int temp = 0;
                        // 从InputStream当中读取客户端所发送的数据
                        while ((temp = inputStream.read(buffer)) != -1) {
                            x = new String(buffer, 0, temp, "UTF-8");
                            Log.e("======读取成功", "service: " + x);

                            if (x.equals(Contance.OPEN_CAMERA)) {
                                Intent intent = new Intent(MyServerSocketService.this, CaptureActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);

                            }
                            /*接受到手机端发来的指令，判断*/
                            if (x.equals(Contance.TAKE_PIC)) {
                                EventBus.getDefault().post(new MessageEvent(x, socket, Contance.CONTROLCAP));

                            }
                            if (x.equals(Contance.CALLBACKPIC)) {
                                Log.e("-----", "run: 回传照片");
                                EventBus.getDefault().post(new MessageEvent(x, socket, Contance.CONTROLCAP));

                            }
//                                EventBus.getDefault().post(new MessageEvent(x, Contance.READSUCCESS));
                        }
                        input.close();

                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println(e.getCause());
                    } finally {
//                if (socket != null) {
//                    try {
//                        socket.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
                    }
                }
            }
        }).start();

        return Service.START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
